﻿#include "leftdockwindow.h"
extern void setButtonStyle(QPushButton *pushButton, const QString &imageUrl1, const QString &imageUrl2);
LeftDockWindow::LeftDockWindow(QWidget *parent, const Qt::AlignmentFlag &v_alignment, const Qt::AlignmentFlag &h_alignment, const QPoint &pos)
    : StackWindow(parent, v_alignment, h_alignment, pos)
{
    //----------initalize widget
    widget=new QWidget(this);
    widget->setFixedSize(100,600);
    this->setFixedSize(400,600);
    mouseFilter=new MouseFilter(this);

    this->installEventFilter(mouseFilter);

    leftSideButton=new QPushButton(this);
    leftSideButton->setFixedSize(25,30);
    isShow = false;
    // 设置按钮样式跟随侧边栏状态而变化
    changeLeftButtonStyle(false);
    //直接移动侧边栏无效，可能是界面重绘不支持。所以间隔0.2秒后再移动
    QTimer::singleShot(200, this, SLOT(slotMoveWidget()));
    //初始化动画
    anim1 = new QPropertyAnimation(widget, "geometry");
    anim1->setEasingCurve(QEasingCurve::Linear);
    anim1->setDuration(500);
    anim2 = new QPropertyAnimation(leftSideButton, "geometry");
    anim2->setEasingCurve(QEasingCurve::Linear);
    anim2->setDuration(500);
    widget->move(-widget->width(), 0);
    connect(leftSideButton,&QPushButton::clicked,this,&LeftDockWindow::leftSideButtonClicked);


    QString *imageurl1=new QString(":/image/hangpai_5.svg");
    QString *imageurl2=new QString(":/image/hangpai_2.svg");

    QString *imageurl3=new QString(":/image/hangpai_3.svg");
    QString *imageurl4=new QString(":/image/hangpai_1.svg");

    QString *imageurl5=new QString(":/image/hangpai_4.svg");
    QString *imageurl6=new QString(":/image/hangpai.svg");

    QString *imageurl7=new QString(":/image/diqiu_1.svg");
    QString *imageurl8=new QString(":/image/diqiu.svg");

    QString *imageurl9=new QString(":/image/box.svg");
    QString *imageurl10=new QString(":/image/box_o.svg");

    plottingbtnClick=false;
    identificationbtnClick=false;
    planningbtnClick=false;
    plottingButton = new TargetPlottingButton(this->widget);
    identiButton = new IdentificationButton(this->widget);
    missionButton = new MissionPlanningButton(this->widget);
    detectionButton = new DetectionButton(this->widget);

    QPushButton *q5 = new QPushButton(this->widget);

    QSize size=QSize(80,80);
    plottingButton->setFixedSize(size);
    identiButton->setFixedSize(size);
    missionButton->setFixedSize(size);
    detectionButton->setFixedSize(size);
    q5->setFixedSize(size);

    setButtonStyle(plottingButton,*imageurl1,*imageurl2);
    setButtonStyle(identiButton,*imageurl3,*imageurl4);
    setButtonStyle(missionButton,*imageurl5,*imageurl6);
    setButtonStyle(detectionButton,*imageurl7,*imageurl8);
    setButtonStyle(q5,*imageurl9,*imageurl10);

    QVBoxLayout *layout = new QVBoxLayout(widget);
    layout->addWidget(plottingButton);
    layout->addStretch();
    layout->addWidget(identiButton);
    layout->addStretch();
    layout->addWidget(missionButton);
    layout->addStretch();
    layout->addWidget(detectionButton);
    layout->addStretch();
    layout->addWidget(q5);
    layout->addStretch();

    plottingButton->plottingWidget->hide();
    identiButton->identificationWidget->hide();
    missionButton->widget->hide();
    detectionButton->detectionWidget->hide();
    plottingButton->plottingWidget->setParent(this);
    identiButton->identificationWidget->setParent(this);
    missionButton->widget->setParent(this);
    detectionButton->detectionWidget->setParent(this);
    //set the layout alignment
    layout->setAlignment(_v_alignmentFlag | _h_alignmentFlag);
    widget->setVisible(true);
    widget->setLayout(layout);
    widget->setStyleSheet("background-color: rgba(0,106,255, 0.3);");//set background color

    connect(plottingButton, &QPushButton::clicked, [=](){
        emit startPlot();
    });
    connect(plottingButton,&QPushButton::clicked,[=](){
        if(plottingbtnClick==false){
            plottingButton->plottingWidget->show();
            identiButton->identificationWidget->hide();
            missionButton->widget->hide();
            detectionButton->detectionWidget->hide();
            detectionbtnClick=false;
            plottingbtnClick=true;
            identificationbtnClick=false;
            planningbtnClick=false;

        }
        else if(plottingbtnClick==true){
            plottingButton->plottingWidget->hide();
            plottingbtnClick=false;
        }
    });

    connect(identiButton,&QPushButton::clicked,[=](){
        if(identificationbtnClick==false){
            identiButton->identificationWidget->show();
            missionButton->widget->hide();
            plottingButton->plottingWidget->hide();
            detectionButton->detectionWidget->hide();
            detectionbtnClick=false;
            identificationbtnClick=true;
            plottingbtnClick=false;
            planningbtnClick=false;
        }
        else{
            identiButton->identificationWidget->hide();
            identificationbtnClick=false;
        }
    });

    connect(missionButton,&QPushButton::clicked,[=](){
        if(planningbtnClick==false){
            missionButton->widget->show();
            identiButton->identificationWidget->hide();
            plottingButton->plottingWidget->hide();
            detectionButton->detectionWidget->hide();
            detectionbtnClick=false;
            planningbtnClick=true;
            identificationbtnClick=false;
            plottingbtnClick=false;
        }
        else{
            missionButton->widget->hide();
            planningbtnClick=false;
        }
    });
    connect(detectionButton,&QPushButton::clicked,[=](){
           if(detectionbtnClick==false){
               detectionButton->detectionWidget->show();
               identiButton->identificationWidget->hide();
               plottingButton->plottingWidget->hide();
               missionButton->widget->hide();
               detectionbtnClick=true;
               identificationbtnClick=false;
               plottingbtnClick=false;
               planningbtnClick=false;
           }
           else{
               detectionButton->detectionWidget->hide();
               detectionbtnClick=false;
           }
       });
}
/* 设置按钮样式跟随侧边栏状态而变化 */
void LeftDockWindow::changeLeftButtonStyle(bool isShowSide)
{
    QString qss;
    if(isShowSide) {
        qss = "QPushButton {border-image: url(:/image/ic_left_normal.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/ic_left_normal.png);}"
                "QPushButton:pressed {border-image: url(:/image/ic_left_pressed.png);}";
    } else {
        qss = "QPushButton {border-image: url(:/image/ic_right_normal.png);border-radius: 6px;}"
                "QPushButton:hover {border-image: url(:/image/ic_right_normal.png);}"
                "QPushButton:pressed {border-image: url(:/image/ic_right_pressed.png);}";
    }
    this->leftSideButton->setStyleSheet(qss);
}
void LeftDockWindow::leftSideButtonClicked()
{
    // 侧边栏和按钮动画
    if(isShow) {
        isShow = false;
        anim1->setStartValue(QRect(0, 0, widget->width(), widget->height()));
        anim1->setEndValue(QRect(-widget->width(), 0, widget->width(), widget->height()));

        anim2->setStartValue(QRect(widget->width(), 0, widget->width(), widget->height()));
        anim2->setEndValue(QRect(0, 0, widget->width(), widget->height()));
    } else {
        isShow = true;
        anim1->setStartValue(QRect(-widget->width(), 0, widget->width(), widget->height()));
        anim1->setEndValue(QRect(0, 0, widget->width(), widget->height()));

        anim2->setStartValue(QRect(0, 0, widget->width(), widget->height()));
        anim2->setEndValue(QRect(widget->width(), 0, widget->width(), widget->height()));
    }
    anim1->start();
    anim2->start();

    changeLeftButtonStyle(isShow);
}

void LeftDockWindow::slotMoveWidget()
{
    widget->move(widget->width(), 0);
}
void setButtonStyle(QPushButton *pushButton, const QString &imageUrl1, const QString &imageUrl2) {
    QString buttonStyle = "QPushButton{"
            "border-image:url(%1);"
            "border-style:outset;"
            "border-width:4px;"
            "border-radius:10px;"
            "border-color:rgba(255,255,255,50);"
            "background-color:rgba(211,211,211,0.3);"
            "}"
            "QPushButton:pressed{"
            "background-color:rgba(100,255,100,0);"
            "border-color:rgba(255,255,255,50);"
            "border-style:outset;"
            "color:rgba(0,0,0,100);"
            "}"
            "QPushButton:hover{"
            "border-image:url(%2);"
            "border-color:rgba(255,255,255,200);"
            "color:rgba(0,0,0,200);"
            "}";

    pushButton->setStyleSheet(buttonStyle.arg(imageUrl1).arg(imageUrl2));
}
